package com.junlt.p2p.httpmanage.simplehttp;

import com.junlt.p2p.entity.BaseResultEntity;
import com.junlt.p2p.ui.base.BaseRxAppCompatActivity;

import java.lang.ref.SoftReference;

import io.reactivex.Observable;
import io.reactivex.functions.Function;
import retrofit2.Retrofit;

/**
 * Created by tj on 2018/1/3.
 */
//实现function进行转换
public abstract class BaseApi<T> implements Function<BaseResultEntity<T>, T> {
    //rx生命周期管理
    private SoftReference<BaseRxAppCompatActivity> rxAppCompatActivity;
    /*回调*/
    private SoftReference<HttpOnNextListener> listener;
    /*是否能取消加载框*/
    private boolean cancel;
    /*是否显示加载框*/
    private boolean showProgress;
    /*是否需要缓存处理*/
    private boolean cache;
    /*基础url*/
    private  String baseUrl="https://www.junlt.com/mapp/";
    /*方法-如果需要缓存必须设置这个参数；不需要不用設置*/
    private String mothed;
    /*超时时间-默认6秒*/
    private int connectionTime = 6;
    /*有网情况下的本地缓存时间默认60秒*/
    private int cookieNetWorkTime=60;
    /*无网络的情况下本地缓存时间默认30天*/
    private int cookieNoNetWorkTime=24*60*60*30;
    /*方法-如果需要缓存必须设置这个参数；不需要不用設置*/
    private String method="";

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        /*在没有手动设置url情况下，简单拼接*/
        if (null == getCacheUrl() || "".equals(getCacheUrl())) {
            return getBaseUrl() + getMethod();
        }
        return getCacheUrl();
    }
    public void setRxAppCompatActivity(SoftReference<BaseRxAppCompatActivity> rxAppCompatActivity) {
        this.rxAppCompatActivity = rxAppCompatActivity;
    }

    public void setListener(SoftReference<HttpOnNextListener> listener) {
        this.listener = listener;
    }

    public String getCacheUrl() {
        return cacheUrl;
    }

    public void setCacheUrl(String cacheUrl) {
        this.cacheUrl = cacheUrl;
    }

    /*缓存url-可手动设置*/
    private String cacheUrl;

    public BaseApi(HttpOnNextListener listener, BaseRxAppCompatActivity baseRxAppCompatActivity) {
        setListener(listener);
        setRxAppCompatActivity(baseRxAppCompatActivity);
        setShowProgress(true);
        setCache(true);
    }

    public BaseRxAppCompatActivity getRxAppCompatActivity() {
        return rxAppCompatActivity.get();
    }

    public void setRxAppCompatActivity(BaseRxAppCompatActivity baseRxAppCompatActivity) {
        this.rxAppCompatActivity = new SoftReference(baseRxAppCompatActivity);
    }

    public SoftReference<HttpOnNextListener> getListener() {
        return listener;
    }

    public void setListener(HttpOnNextListener listener) {
        this.listener = new SoftReference(listener);
    }

    public boolean isCancel() {
        return cancel;
    }

    public void setCancel(boolean cancel) {
        this.cancel = cancel;
    }

    public boolean isShowProgress() {
        return showProgress;
    }

    public void setShowProgress(boolean showProgress) {
        this.showProgress = showProgress;
    }

    public boolean isCache() {
        return cache;
    }

    public void setCache(boolean cache) {
        this.cache = cache;
    }

    public String getBaseUrl() {
        return baseUrl;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public String getMothed() {
        return mothed;
    }

    public void setMothed(String mothed) {
        this.mothed = mothed;
    }

    public int getConnectionTime() {
        return connectionTime;
    }

    public void setConnectionTime(int connectionTime) {
        this.connectionTime = connectionTime;
    }

    public int getCookieNetWorkTime() {
        return cookieNetWorkTime;
    }

    public void setCookieNetWorkTime(int cookieNetWorkTime) {
        this.cookieNetWorkTime = cookieNetWorkTime;
    }

    public int getCookieNoNetWorkTime() {
        return cookieNoNetWorkTime;
    }

    public void setCookieNoNetWorkTime(int cookieNoNetWorkTime) {
        this.cookieNoNetWorkTime = cookieNoNetWorkTime;
    }

    /**
     * 设置参数
     *
     * @param retrofit
     * @return
     */
    public abstract Observable getObservable(Retrofit retrofit);

    //数据转换，将状态码统一处理
    @Override
    public T apply(BaseResultEntity<T> BaseResultEntity) throws Exception {
        if (BaseResultEntity.getStatusCode() != 0) {

        }
        return BaseResultEntity.getData();
    }

}
